/**
 * Social Comments
 * Implements the same CRUD terminology
 *
 * @author Seth
 * @version 0.5
 */
Social.Comments = function(){
	var s = {},
		max = 200,
		page = 1,

		// create (C)
		_create = function() {
			var comment = s.loc.find('[name="comment_body"]').val();

			_showLoading();
	    	Petolio.ajax({
				url: 'social/create',
				type: 'post',
				data: {id: s.id, scope: s.scope, plugin: 'comments', comment: comment, label: s.label, url: s.url, owner: s.owner},
				cache: false,
				success: function (x) {
					page = 1;
					_read();
					_hideLoading();
					Social.load('subscriptions', s.win, {id: s.id, scope: s.scope});
				}
			});

		// read (R)
		},	_read = function() {
			_showLoading();
	    	Petolio.ajax({
				url: 'social/read',
				type: 'post',
				data: {id: s.id, scope: s.scope, plugin: 'comments', page: page},
				cache: false,
				success: function (x) {
					s.loc.html(x.html);
					_bind();
					Social.done();
					_hideLoading();
				}
			});

		// update (U)
		},	_update = function() {
			// not used

		// delete (D)
		},	_delete = function(e) {
			var id = $(this).parent().attr('rel');

			Petolio.showConfirm(_translate('delete_comment'), function() {
				_showLoading();
		    	Petolio.ajax({
					url: 'social/delete',
					type: 'post',
					data: {x: id, id: s.id, scope: s.scope, plugin: 'comments'},
					cache: false,
					success: function (x) {
						_read();
						_hideLoading();
					}
				});
			});

		// bind events
		},	_bind = function() {
			s.loc.find('[name="comment_submit"]').click(_save);
			s.loc.find('[name="comment_delete"]').click(_delete);
			s.loc.find('[name="comment_body"]').focus(_focus);
			s.loc.find('[name="comment_body"]').blur(_blur);
			s.loc.find('[name="comment_body"]').keyup(_limit);
			s.loc.find('[name="comment_body"]').keydown(_limit);
			s.loc.find('[class="comment_pagination"] a').click(_pagination);

		// call create or update
		},	_save = function() {
			var comment = s.loc.find('[name="comment_body"]');

			// reset error
			comment.css({borderColor: '#ccc'});

			// check length
			if(comment.val() == _translate('blank') || !comment.val().length > 0) {
				comment.css({borderColor: 'red'});
				return false;
			}

			_create();

		// textarea focus
		},	_focus = function() {
			if($(this).val() == _translate('blank'))
				$(this).val('');

		// textarea blur
		},	_blur = function() {
			if(!$(this).val())
				$(this).val(_translate('blank'));

		// textarea limit
		},	_limit = function(e) {
			var count = s.loc.find('[class="comment_count"]');

			// limit chars
			if ($(this).val().length > max) {
			    $(this).val($(this).val().substring(0, max));
			    this.scrollTop = this.scrollHeight;
			} else
			    count.html(max - $(this).val().length);

		// pagination handle
		},	_pagination = function(e) {
			var p = parseInt($(this).attr('rel'));

			// only numbers allowed
			if(!isNaN(p))
				page = p;

			_read();

		// translation handle
		},	_translate = function(i) {
			var t = s.loc.find('[class="comment_translate"]'),
				o = $.parseJSON(t.html());

			return o[i];

		// show loading
		},	_showLoading = function() {
			var x = s.loc.find('[class="comment_loading"]');
			x.show();

		// hide loading
		},	_hideLoading = function() {
			var x = s.loc.find('[class="comment_loading"]');
			x.hide();

		// plugin constructor
		},	__construct = function(win, params) {
			s.loc = win.find('.comments');
			s.id = params.id;
			s.scope = params.scope;
			s.label = params.label;
			s.url = params.url;
			s.owner = params.owner;
			s.win = win;

			_read();
		};

	// public
	return {
		load: __construct
	};
}();